using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
namespace MicroMsg.Scene.Voice
{
	public class UploadVoiceContextLoader
	{
		private const string TAG = "UploadVoiceContextLoader";
		private const int MIN_CLEAN_INTERVAL = 20000;
		private const int MIN_RESUM_INTERVAL = 20000;
		private static double mLastCleanTimeStamp;
		private static double mLastLoadTimeStamp;
		public static void resetIntervalLimit()
		{
			UploadVoiceContextLoader.mLastCleanTimeStamp = 0.0;
			UploadVoiceContextLoader.mLastLoadTimeStamp = 0.0;
		}
		public static void checkCleanMsgTrans()
		{
			double nowMilliseconds = Util.getNowMilliseconds();
			if (nowMilliseconds < UploadVoiceContextLoader.mLastCleanTimeStamp + 20000.0)
			{
				Log.smethod_5("UploadVoiceContextLoader", "cannot clean too fast, try later. ");
				return;
			}
			UploadVoiceContextLoader.mLastCleanTimeStamp = nowMilliseconds;
			List<MsgTrans> cleanUploadVoiceList = StorageMgr.msgVoice.getCleanUploadVoiceList(20, (long)UploadVoiceContextLoader.mLastCleanTimeStamp);
			if (cleanUploadVoiceList != null && cleanUploadVoiceList.get_Count() != 0)
			{
				using (List<MsgTrans>.Enumerator enumerator = cleanUploadVoiceList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						MsgTrans current = enumerator.get_Current();
						if (UploadVoiceContextMgr.getInstance().findByClientMsgID(current.strClientMsgId) == null)
						{
							Log.smethod_4("UploadVoiceContextLoader", "cleaning ... " + current.strClientMsgId);
							StorageMgr.msgVoice.method_2(current);
						}
					}
				}
				return;
			}
		}
		public static void checkResumeMsgTrans()
		{
			double nowMilliseconds = Util.getNowMilliseconds();
			if (nowMilliseconds < UploadVoiceContextLoader.mLastLoadTimeStamp + 20000.0)
			{
				Log.smethod_5("UploadVoiceContextLoader", "cannot resume too fast, try later.");
				return;
			}
			UploadVoiceContextLoader.mLastLoadTimeStamp = nowMilliseconds;
			List<MsgTrans> resumeUploadVoiceList = StorageMgr.msgVoice.getResumeUploadVoiceList(5, (long)UploadVoiceContextLoader.mLastLoadTimeStamp);
			if (resumeUploadVoiceList != null && resumeUploadVoiceList.get_Count() != 0)
			{
				using (List<MsgTrans>.Enumerator enumerator = resumeUploadVoiceList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						MsgTrans current = enumerator.get_Current();
						Log.smethod_4("UploadVoiceContextLoader", "resuming.... " + current.strClientMsgId);
						if (UploadVoiceContextMgr.getInstance().findByClientMsgID(current.strClientMsgId) != null)
						{
							Log.smethod_5("UploadVoiceContextLoader", "already running... " + current.strClientMsgId);
						}
						else
						{
							UploadVoiceContext uploadVoiceContext = UploadVoiceContext.createByMsgTrans(current);
							if (!uploadVoiceContext.loadFromMsgTrans())
							{
								Log.smethod_1("UploadVoiceContextLoader", "Failed to load recorder file,  " + current.strClientMsgId);
							}
							else
							{
								Log.smethod_5("UploadVoiceContextLoader", "load a recorder file success, put task to tail. ");
								UploadVoiceContextMgr.getInstance().putToTail(uploadVoiceContext);
							}
						}
					}
				}
				return;
			}
		}
		public static bool resumeByClientMsgID(string clientMsgID)
		{
			MsgTrans byClientMsgID = StorageMgr.msgVoice.getByClientMsgID(clientMsgID);
			if (byClientMsgID == null)
			{
				Log.smethod_1("UploadVoiceContextLoader", "not found task ,clientmsgid =  " + clientMsgID);
				return false;
			}
			UploadVoiceContext.printInfo(byClientMsgID);
			if (!UploadVoiceContext.needResumeFromTrans(byClientMsgID, 0u))
			{
				return false;
			}
			Log.smethod_4("UploadVoiceContextLoader", "resuming.... " + clientMsgID);
			UploadVoiceContext uploadVoiceContext = UploadVoiceContext.createByMsgTrans(byClientMsgID);
			if (!uploadVoiceContext.loadFromMsgTrans())
			{
				Log.smethod_1("UploadVoiceContextLoader", "Failed to load recorder file,  " + byClientMsgID.strClientMsgId);
				return false;
			}
			Log.smethod_5("UploadVoiceContextLoader", "load a recorder file success, put task to head. ");
			UploadVoiceContextMgr.getInstance().putToHead(uploadVoiceContext);
			return true;
		}
		public static void cleanSendingStatus()
		{
			List<ChatMsg> list = StorageMgr.chatMsg.searhCacheMsg(34, 0, 1);
			if (list != null && list.get_Count() > 0)
			{
				using (List<ChatMsg>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ChatMsg current = enumerator.get_Current();
						Log.smethod_4("UploadVoiceContextLoader", "clean status sending to fail , " + current.strClientMsgId);
						current.nStatus = 1;
					}
				}
				StorageMgr.chatMsg.updateMsgList(list);
				return;
			}
		}
	}
}
